<template>
	<view>
		<navbar title="租赁申请" :autoBack="true"></navbar>
		<scroll-view scroll-y="true" class="scbox">
			<view class="con">
				<view class="con__box">
					<view class="con__box__title">
						<image src="/static/daikuan/zlsq.png" class="con__box__title__icon" mode=""></image>
						收货方式
					</view>
					<view class="con__box__item u-flex u-flex-between">
						<view class="con__box__item__inp__r" style="margin-left: 0;" @click="form.method=1">
							<view class="con__box__item__inp__r__b">
								<image src="/static/daikuan/check.png" v-if="form.method==1"
									class="con__box__item__inp__r__b__c" mode="">
								</image>
								<view class="con__box__item__inp__r__b__n" v-else>

								</view>
							</view>
						</view>
						<view class="con__box__item__label" style="color: #010101;">
							上门自提
						</view>
						<view class="con__box__item__inp">
							{{address}}
						</view>
					</view>
					<view class="con__box__item u-flex">
						<view class="con__box__item__inp__r" style="margin-left: 0;" @click="form.method=2">
							<view class="con__box__item__inp__r__b">
								<image src="/static/daikuan/check.png" v-if="form.method==2"
									class="con__box__item__inp__r__b__c" mode="">
								</image>
								<view class="con__box__item__inp__r__b__n" v-else>

								</view>
							</view>
						</view>
						<view class="con__box__item__label" style="color: #010101;">
							邮寄
						</view>
						<view class="con__box__item__label" style="color: #010101;flex: 1;text-align: right;" v-if="addr" @click="navto('/pages/personal/editaddr?id='+addr.id)">
							{{addr.province+addr.city+addr.area+addr.detail}}
						</view>
						<view class="con__box__item__label" style="flex: 1;text-align: right;" @click="navto('/pages/personal/addr')" v-else>
							添加收获地址
						</view>
						<image src="/static/daikuan/more.png" class="con__box__item__more" mode=""></image>
						<!-- <input type="text" class="con__box__item__inp" placeholder="请输入利率" /> -->
					</view>
					<view class="con__box__item">
						<view class="con__box__item__line">

						</view>
						<view class="con__box__item__label" style="color:#1E1E1E;">
							基本信息
						</view>
					</view>
					<view class="con__box__item" v-for="(item,index) in formdata" :key="index">
						<view class="con__box__item__label">
							{{item.label}}
						</view>
						<view class="con__box__item__inp" v-if="item.type=='data'">
							{{item.val}}
						</view>
						<view class="con__box__item__inp" style="color: #6D6D6D;" v-if="item.type=='data'&&!item.val">
							{{item.pl}}
						</view>
						<view class="con__box__item__inp" v-if="item.type=='radio'">
							<view class="con__box__item__inp__r" v-for="(item1,index1) in item.rlist" :key="index1"
								@click="clickr(item,item1)">
								<view class="con__box__item__inp__r__b">
									<image src="/static/daikuan/check.png" v-if="item1.name==item.val"
										class="con__box__item__inp__r__b__c" mode="">
									</image>
									<view class="con__box__item__inp__r__b__n" v-else>

									</view>
								</view>
								{{item1.name}}
							</view>
						</view>
						<input type="text" class="con__box__item__inp" v-if="item.type=='inp'" v-model="item.val"
							:placeholder="'请填写'+item.label" />
						<view class="u-flex u-flex-y-center" v-if="item.type=='check'" style="flex: 1;">
							<view class="" style="flex: 1;" @click="clickto(item)">
								<view class="con__box__item__inp" v-if="item.val">
									{{item.val}}
								</view>
								<view class="con__box__item__inp" style="color: rgb(109, 109, 109);" v-else>
									{{item.pl}}
								</view>
							</view>
							<image src="/static/daikuan/more.png" class="con__box__item__more" mode=""></image>
						</view>
					</view>
					<u-button color="#3262FD" text='提交申请' @click="submit" :loading="loading"
						:customStyle='{width:"628rpx",height:"94rpx",margin:"62rpx 0 0 0",padding:0,borderRadius:"15rpx",fontSize:"32rpx"}'></u-button>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {
		data() {
			return {
				shtype: 1,
				loading:false,
				formdata: [{
						label: '姓名',
						val: '',
						type: 'inp',
						name: 'name'
					},
					{
						label: '电话',
						val: '',
						type: 'inp',
						name: 'mobile'
					},
					{
						label: '销售',
						val: '',
						type: 'check',
						pl: '选择销售',
						name: 'sale',
						url:'/pages/customer/checkxs'
					},
					{
						label: '租赁方式',
						val: '',
						type: 'radio',
						name: 'hiremethod',
						rlist: [{
							name: '租完即送',
							type:1
						}, {
							name: '随租随还',
							type:2
						}]
					},
					{
						label: '租赁期数',
						val: '',
						type: 'radio',
						rlist: [{
							name: '6期',
							type:1,
							val:6
						}, {
							name: '12期',
							type:2,
							val:12
						}, {
							name: '24期',
							type:3,
							val:24
						}],
						name: 'hirenum'
					},
					{
						label: '每期租金',
						val: '',
						type: 'data',
						pl: '选择期数显示每期租金',
						name: 'rent'
					},
					{
						label: '绑定银行卡',
						val: '',
						type: 'check',
						pl: '选择银行卡',
						url: '/pages/personal/bindcard',
						name: 'bank'
					},
				],
				form: {
					method:'1',
					goods_id:null,
					spec_json:null,
					num:1,
					price:0,
					name:'',
					mobile:'',
					sale_id:'',
					hire_method:'',
					hire_num:'',
					rent:'',
					bank:'',
				},
				address:'',
				addr:null
			};
		},
		methods: {
			async submit(){
				this.formdata.map(t=>{
					for (let key in this.form) {
						if(t.name==key){
							this.form[key]=t.val
						}
					}
				})
				if (!this.paramsValidate(this.form)) {
					return
				}
				let _this=this
				_this.loading=true
				let res = await user.createOrder(this.form)
				if(res.data.code==1){
					uni.$u.toast('订单提交成功，等待审核')
					setTimeout(()=>{
						_this.loading=false
						// _this.navto('/pages/lease/order')
						uni.redirectTo({
							url:'/pages/lease/order'
						})
					},1000)
				}
			},
			paramsValidate(params) {
				let flag = true;
				for (var key in params) {
					if (params[key] != '0' && !params[key]) {
						uni.$u.toast('请将信息填写完整')
						return false; // 终止程序
					}
				}
				return flag;
			},
			async clickr(item,item1){
				console.log(item,item1)
				item.val=item1.name
				if(item.name=='hiremethod'){
					this.form.hire_method=item1.type
					
				}
				if(item.name=='hirenum'){
					this.form.hire_num=item1.type
					// 计算每期租金
					let res = await user.meterAmount({qishu:item1.val,money:this.form.price*this.form.num})
					if(res.data.code==1){
						this.formdata.map(t=>{
							if(t.name=='rent'){
								t.val=res.data.data.everyPrice.toFixed(2)
							}
						})
					}
				}
			},
			clickto(item) {
				if (item.url) {
					this.navto(item.url)
				}
			},
			async getaddr(){
				let res = await user.defaultAddress()
				if(res.data.code==1){
					this.addr=res.data.data
				}
			},
			async getbank(){
				let res = await user.getMyBank()
				let bank={}
				if(res.data.code==1){
					bank=res.data.data
					if(!bank){
						return
					}
					this.formdata.map(t=>{
						if(t.name=='bank'){
							t.val=bank.cardno
						}
					})
				}
			}
		},
		onLoad() {
			console.log(this.$store.state.curgoods)
			let goods = this.$store.state.curgoods
			this.address=goods.address
			this.form.spec_json=goods.spec_json
			this.form.num=goods.num
			this.form.price=goods.price
			this.form.price=goods.price
			this.form.goods_id=goods.goods_id
		},
		onShow() {
			this.getaddr()
			this.getbank()
			if(this.$store.state.curuser){
				this.formdata.map(t=>{
					if(t.name=='sale'){
						t.val=this.$store.state.curuser.manager_name
					}
				})
				this.form.sale_id=this.$store.state.curuser.manager_id
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;
	}

	.con {
		padding: 20rpx 30rpx 50rpx;

		&__box {
			background: #FFFFFF;
			border-radius: 30rpx;
			padding: 33rpx 0 46rpx 30rpx;

			&__title {
				display: flex;
				align-items: center;
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #1E1E1E;
				line-height: 30rpx;

				&__icon {
					width: 40rpx;
					height: 40rpx;
					background: #45C47B;
					border-radius: 50%;
					margin-right: 14rpx;
				}
			}

			&__ti {
				font-family: PingFang SC;
				font-weight: 500;
				font-size: 26rpx;
				color: #1E1E1E;
				line-height: 26rpx;
				display: flex;
				align-items: center;
				padding: 35rpx 0 30rpx;
				padding-left: 10rpx;

				&__line {
					width: 6rpx;
					height: 24rpx;
					background: #3262FD;
					margin-right: 10rpx;
				}
			}

			&__money {
				display: flex;
				font-family: PingFang SC;
				font-weight: 800;
				font-size: 36rpx;
				color: #010101;
				line-height: 40rpx;
				align-items: center;
				margin: 30rpx 0 0;
				padding-bottom: 20rpx;
				border-bottom: 2rpx solid #F9F9F9;

				// margin-top: 10rpx;
				&__inp {
					font-family: PingFang SC;
					font-weight: 800;
					font-size: 53rpx;
					color: #000000;
					line-height: 40rpx;
					margin-left: 6rpx;
				}
			}

			&__item {
				height: 106rpx;
				padding: 0 30rpx 0 0;
				display: flex;
				align-items: center;
				// justify-content: space-between;
				border-bottom: 2rpx solid #F9F9F9;

				&__more {
					width: 12rpx;
					height: 21rpx;
					margin-left: 13rpx;
				}

				&__line {
					width: 5rpx;
					height: 24rpx;
					background: #3262FD;
					margin-right: 10rpx;
				}

				&__label {
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #6D6D6D;
					line-height: 26rpx;
				}

				&__inp {
					flex: 1;
					font-family: PingFang SC;
					font-weight: 500;
					font-size: 26rpx;
					color: #020202;
					line-height: 38rpx;
					text-align: right;
					display: flex;
					align-items: center;
					justify-content: flex-end;

					.uni-input-placeholder {
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #6D6D6D;
						line-height: 38rpx;
					}

					&__timelabel {
						font-family: PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #6D6D6D;
						line-height: 38rpx;
					}

					&__r {
						display: flex;
						align-items: center;
						margin-left: 50rpx;

						&__b {
							display: flex;
							align-items: center;
							justify-content: center;
							margin-right: 9rpx;

							&__c {
								width: 28rpx;
								height: 28rpx;
								border-radius: 50%;
							}

							&__n {
								width: 24rpx;
								height: 24rpx;
								border-radius: 50%;
								border: 2rpx solid #6D6D6D;
								opacity: 0.5;
							}
						}
					}
				}
			}
		}
	}

	::v-deep.u-textarea {
		padding: 32rpx 30rpx 60rpx 0;

		.u-textarea__field {
			font-size: 26rpx;
			color: #020202;
		}

		.uni-textarea-placeholder {
			color: #939393 !important;
		}
	}
</style>